package likou;

import java.util.ArrayList;

public class likou {
    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
        int l1 = nums1.length;
        int l2 = nums2.length;
        int[] newNums = new int[l1+l2];
        System.arraycopy(nums1,0,newNums,0,l1);
        System.arraycopy(nums2,0,newNums,l1,l2);
        sort(newNums,0,l1+l2-1);
        if ((l2 + l1) % 2 != 0) {
            int a = (l2 + l1) / 2;
            return newNums[a];
        }else {
            int b = (l1 + l2)/ 2;
            return (newNums[b] + newNums[b-1])/2.0;
        }
    }

    public static void sort(int[] arr,int low,int high) {
        if (low >= high){
            return;
        }
        int left = low;
        int rgt = high;
        int pivot = arr[low];

        while (left < rgt){
            while (left < rgt && arr[rgt] > pivot){
                rgt--;
            }

            if (left < rgt){
                arr[left] = arr[rgt];
            }
            while (left < rgt && arr[left] < pivot){
                left++;
            }

            if (left < rgt){
                arr[rgt] = arr[left];
                rgt--;
            }
        }

        arr[left] = pivot;
        sort(arr,low,left-1);
        sort(arr,left+1, high);
    }

    public static void main(String[] args) {
        int[] n1=new int[]{11,22,2,12,99,34};
        int[] n2=new int[]{10,3,66,77,33};
        likou likou = new likou();
        double c = likou.findMedianSortedArrays(n1,n2);
        System.out.println(c);
    }
}
